<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 任务看板{{kanban.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-input v-model="query.name" placeholder="任务名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-search" @click="handleCreate">新增</el-button>
                <el-button type="primary" icon="el-icon-search" @click="handleShare">分享</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="任务名"></el-table-column>
                  <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.isComplete?'success':'danger'"
                        >{{scope.row.isComplete?"已完成":"未完成"}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="createdUserName" label="创建人"></el-table-column>

                <el-table-column prop="createdTime" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleComplete(scope.$index, scope.row)"
                        >完成</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <!-- <el-dialog @close="getData()" ref="editmodal"  :visible.sync="editVisible" width="40%"> -->
        <createMisson
            v-if="editVisible"
            :visible.sync="editVisible"
            @on-save-click="getData"
            :itemName="form.name"
            :itemId="form.id"
            :itemKanbanId="kanban.id"
        ></createMisson>
        <shareMissonList
            v-if="shareVisible"
            :visible.sync="shareVisible"
            @on-save-click="getData"
            :itemName="kanban.name"
            :itemId="kanban.id"
        ></shareMissonList>
   
    </div>
</template>

<script>
import { fetchData } from '@/api/index';
import { getall, remove ,complete} from '@/api/misson.js';
import createMisson from './components/create';
import shareMissonList from './components/share';
export default {
    name: 'kanban',
    components: {
        createMisson,shareMissonList
    },
    data() {
        return {
            query: {
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            params: {
                name: ''
            },
            kanban:{

            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            shareVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    mounted(){
        let params = this.$route.params;
        if(!params||!params.kanban){
            this.$message.error("操作错误");
            this.$router.push("/kanban");
        }else{
            this.kanban = params.kanban;
            this.query.kanbanId = params.kanban.id;
        }
    },
    methods: {
        getData() {
            this.editVisible = false;
            this.shareVisible=false;
            getall(this.query).then(res => {
                console.log(res);
                this.tableData = res.data;
                this.pageTotal = res.totalCount || 10;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        //新增
        handleCreate() {
            this.editVisible = true;
        },
       
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    remove({id:row.Id})
                        .then(rep => {
                            this.$message.success('删除成功');
                            this.getData();
                        })
                        .catch(error => {
                            this.$message.success('删除失败：' + error);
                        });
                })
                .catch(() => {});
        },   // 删除操作
        handleComplete(index, row) {
            // 二次确认删除
            this.$confirm('确定要标记为完成吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    complete({id:row.id})
                        .then(rep => {
                            this.$message.success('标记成功');
                            this.getData();
                        })
                        .catch(error => {
                            this.$message.success('标记失败：' + error);
                        });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
    
        // 编辑操作
        handleEdit(index, row) {
            this.hackReset = false;
            this.$nextTick(() => {
                this.hackReset = true;
            });
            this.editVisible = true;
        },
         //分享
        handleShare() {
debugger
            this.shareVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
